﻿using System;
using System.Windows.Controls;
using MPlayer.Common;

namespace FrontDisplay.Controls
{
    /// <summary>
    /// Interaction logic for PositionControl.xaml
    /// </summary>
    public partial class PositionControl : UserControl
    {
        private double _position;

        public double Position
        {
            get { return _position; }
            set
            {
                if ((value < 0) || (value > Duration)) return;

                _position = value;

                TimeLabel.Text = TimeSpan.FromSeconds(_position).Formatted(true);
                ElapsedTimeLabel.Text = TimeSpan.FromSeconds(_duration - _position).Formatted(true);

                if (_duration != 0)
                {
                    PositionBar.Width = (_position/_duration)*ActualWidth;
                    PositionBarBottom.Width = PositionBar.Width;
                }
            }
        }

        private double _duration;

        public double Duration
        {
            get { return _duration; }
            set
            {
                if (value < 0) return;

                _duration = value;
            }
        }

        public PositionControl()
        {
            InitializeComponent();
        }

        public void ClearTime()
        {
            TimeLabel.Text = null;
            ElapsedTimeLabel.Text = null;
            PositionBar.Width = 0;
            PositionBarBottom.Width = 0;
        }

        public void UpdateTime(int position, int duration)
        {
            if (duration > 0)
            {
                Duration = duration;
                Position = position;
            }
            else
            {
                ClearTime();
            }
        }
    }
}
